<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Gate::define('create-event',function($user){
            return $user->role == 'ADMIN';
        });
        Gate::define('modify-event',function($user,$event){
            return $user->role == 'ADMIN' && $event->creator_id == $user->id;
        });
        Gate::define('invite-attendee',function($user,$event){
            return $user->role == 'ADMIN' && $event->creator_id == $user->id  && $event->start_at->isFuture();
        });
        Gate::define('join-event',function($user,$event){
            if($event->type == 'PUBLIC' && $event->start_at->isFuture() && !$user->joined($event)){
                return true;
            }
            if($event->type == 'PRIVATE'
                && $event->start_at->isFuture()
                && $user->hasInvitation($event)
                && !$user->joined($event)
            ){
                return true;
            }
            return false;
        });
    }

}
